package com.tianji.learning.listener;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tianji.api.client.course.CourseClient;
import com.tianji.api.dto.course.CourseSimpleInfoDTO;
import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.exceptions.BizIllegalException;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.domain.entity.LearningLesson;
import com.tianji.learning.service.ILearningLessonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class LapsedCoursesListener {
    @Autowired
    private CourseClient courseClient;
    @Autowired
    private ILearningLessonService lessonService;

    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(name = MqConstants.Queue.ORDER_LESSON_LAPSED_QUEUE),
            exchange = @Exchange(name = MqConstants.Exchange.ORDER_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.ORDER_REFUND_KEY
    ))
    public void onLapsedCourses(OrderBasicDTO dto) {
        log.info("收到课程失效消息，订单id：{}", dto);
        //TODO day02 homework  1.1   orderId:退款的订单id  userId:用户id  courseIds:退款的课程id
        LambdaQueryWrapper<LearningLesson> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LearningLesson::getUserId, dto.getUserId());
        queryWrapper.eq(LearningLesson::getCourseId, dto.getCourseIds());
        lessonService.remove(queryWrapper);
    }
}